/**
 * Created with IntelliJ IDEA.
 * User: teddy zhang
 * Date: 2015-04-17
 * Time: 11:25
 */

define(function (require, exports, module) {
    'use strict';
    
    var rk = require('rk');
    
    $.widget('rk.usersettingEduCtrl', {
        options: {
            selector:null
        },

        _create: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;

            $.postJson('/json/oa_profile-edit/edu.action').done(function (json) {
                if (json.status == 0) {
                    var data = json.data;
                    me._render(data);
                    me._bind();

                }
            }).fail(function (def, code, message) {
                def.status != 0 && rk.noticeError(rk.i18n('REQUEST_ERROR'));
            });


        },

        _render: function (data) {
            var me = this;
            var elem = me.element;
            var opt = me.options;

            var options_year = [];
            var options_month= [];
            var currentYear = moment().format('YYYY');
            for (var year = 1970; year <= 2020; year++) {
                var selected = '';
                if (currentYear == year) selected = 'selected="selected"';
                options_year.push('<option ' + selected + ' value="' + year + '">' + year + '</option>');
            }
            for (var month = 1; month <= 12; month++) {
                options_month.push('<option ' + selected + ' value="' + month + '">' + month + '</option>');
            }

            data.options_year = options_year;
            data.options_month = options_month;


            $(elem).html(rk.templateText(require('page/tmpl/usersetting/tpl_user_setting_edu.tpl'), data));

        },

        _init: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;
        },

        _bind: function () {
            var me = this;
            var elem = me.element;
            var opt = me.options;

            $("#usersetting_check_fomat").buttonset();
            $("#usersetting_select_fomat select").selectmenu({
                    position: {collision: "flipfit"},
                    width: 500
                }
            );


            $(elem).on('click', 'a[act = "edu_delete"]', function (event) {
                event.preventDefault();
                var $this = $(this);
                var edu_id = $this.attr('data-id');
                $.postJson('/json/oa_profile-edit/delete-edu.action', {schoolExpProfileId: edu_id}).done(function (json) {
                    if (json.status == 0) {
                        $this.parent().parent().off().remove();

                    }
                });
                return false;
            });


            $(elem).on('click', 'a[act = "edu_edit"]', function (event) {
                event.preventDefault();
                var $this = $(this);
                var edu_id = $this.attr('data-id');
                var editData = {};
                $('div[data-id= "' + edu_id + '"]').find('span[data-name]').each(function (index, item) {
                    editData[$(item).attr('data-name')] = $(item).attr('data-value')
                });
                $('.past-add').attr('data-id', edu_id);
                $('.past-add').find('input[data-name="schoolName"]').val(editData['schoolName']);
                $('.past-add').find('input[data-name="major"]').val(editData['major']);
                $('.past-add').find('select[data-name="startYear"]').find('option[value=' + editData.startYear + ']').attr("selected", true);

                $('.past-add').find('select[data-name="startMonth"]').find('option[value=' + editData.startMonth + ']').attr("selected", true);
                $('.past-add').find('select[data-name="endYear"]').find('option[value=' + editData.endYear + ']').attr("selected", true);
                $('.past-add').find('select[data-name="endMonth"]').find('option[value=' + editData.endMonth + ']').attr("selected", true);

                $('.past-add').find('select').selectmenu("refresh");
                Number(editData.yetFlg) ? $('.past-add').find('input[data-name="yetFlg"]').attr('checked', true).button("refresh") : $('.past-add').find('input[data-name="yetFlg"]').removeAttr("checked").button("refresh");
                return false;
            });


            $(elem).on('click', 'a[act = "edu_save_btn"]', function (event) {
                event.preventDefault();
                var $this = $(this);
                if($.trim($('.past-add').find('input[data-name="schoolName"]').val()) == ''){
                    $(elem).find('span.nc_error').text(rk.i18n('USERSET_NEED_SCHOOLNAME'));
                    return;
                }
                var postData = {};
                postData['schoolExpProfile.schoolName'] = $('.past-add').find('input[data-name="schoolName"]').val();
                postData['schoolExpProfile.major'] = $('.past-add').find('input[data-name="major"]').val();
                postData['schoolExpProfile.startYear'] = $('.past-add').find('select[data-name="startYear"]').val();
                postData['schoolExpProfile.startMonth'] = $('.past-add').find('select[data-name="startMonth"]').val();
                postData['schoolExpProfile.endYear'] = $('.past-add').find('select[data-name="endYear"]').val();
                postData['schoolExpProfile.endMonth'] = $('.past-add').find('select[data-name="endMonth"]').val();
                postData['schoolExpProfile.yetFlg'] = $('.past-add').find('input[data-name="yetFlg"]').attr('checked') ? 1 : 0;
                postData['schoolExpProfile.id'] = $('.past-add').attr('data-id');

                var postUrl = $('.past-add').attr('data-id')? '/json/oa_profile-edit/update-edu.action':'/json/oa_profile-edit/add-edu.action'
                $.postJson(postUrl, postData).done(function (json) {
                    if (json.status == 0) {
                        opt.selector.trigger('click');
                        rk.noticeSuccess(rk.i18n('USERSET_NEED_EDU_EXPERIENCE'));
                    }
                });
                return false;
            });


        }

    });

    return $;

});

